/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.edu.ifrn.Dao;

import br.edu.ifrn.Classes.Conexao;
import br.edu.ifrn.Classes.Cota;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author cp
 */
public class CotasDao {

    private Connection connection;

    public CotasDao() {
        this.connection = new Conexao().getConnection();
    }

    public void InserirCotas(Cota cotas) {
        String sql = "insert into Cotas(idCota,nome,quantidadeFolhas)" + "values(?,?,?)";

        try {
            PreparedStatement stmt = connection.prepareStatement(sql);

            stmt.setInt(1, cotas.getIdCota());
            stmt.setString(2, cotas.getNome());
            stmt.setInt(3, cotas.getQuantidadeFolhas());
            stmt.execute();
            stmt.close();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public void RemoverCotas(Cota cotas) {
        try {
            PreparedStatement stmt = connection.prepareStatement("delete from Cotas where idCota=?");
            stmt.setInt(1, cotas.getIdCota());
            stmt.execute();
            stmt.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    
    public void EditarCotas(Cota cotas){
        String sql = "UPDATE Cotas SET nome=?, quantidadeFolhas=? WHERE idCota=?";
        
        try{
            java.sql.PreparedStatement stmt = this.connection.prepareStatement(sql);
            stmt.setString(1, cotas.getNome());
            stmt.setInt(2, cotas.getQuantidadeFolhas());
            stmt.setInt(3, cotas.getIdCota());
            
            stmt.execute();
            stmt.close();
        }catch(SQLException e){
            throw new RuntimeException(e);            
        }
    }
    
     public List<Cota> getLista() {
        try {
            List<Cota> cotas = new ArrayList<Cota>();
            PreparedStatement stmt = this.connection.prepareStatement("select * from Cotas");
            ResultSet rs = stmt.executeQuery();

            while (rs.next()) {
                // criando o objeto Contato
                
                Cota cota = new Cota();
                cota.setIdCota(rs.getInt("IdCota"));
                cota.setNome(rs.getString("nome"));
                cota.setQuantidadeFolhas(rs.getInt("quantidadeFolhas"));

                // adicionando o objeto à lista
                cotas.add(cota);
                
            }
            rs.close();
            stmt.close();
            return cotas;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
